﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class sound_manager : UnitySingleton<sound_manager> {

    const int MAX_SOUNDS = 8;
    private AudioSource music;
    private AudioSource[] sounds = new AudioSource[MAX_SOUNDS];

    #region 这四个参数需要保存到本地
    private int music_mute;//静音
    private int sound_mute;

    private float music_volume;
    private float sound_volume;
    #endregion
    private int now_soundid = 0;

    public void Init()
    {
        music_mute = PlayerPrefs.GetInt("music_mute", 0);
        sound_mute = PlayerPrefs.GetInt("sound_mute", 0);
        music_volume = PlayerPrefs.GetFloat("music_volume", 1.0f);
        sound_volume = PlayerPrefs.GetFloat("sound_volume", 1.0f);
        

        music = gameObject.AddComponent<AudioSource>();
        music.mute = (music_mute == 1);
        music.volume = music_volume;


        for (int i = 0; i < MAX_SOUNDS; i++)
        {
            sounds[i] = gameObject.AddComponent<AudioSource>();
            sounds[i].mute = (sound_mute == 1);
            sounds[i].volume = sound_volume;
        }
    }

    public void play_music(string url, bool isLoop = true)
    {
        AudioClip clip = Resources.Load<AudioClip>(url);
        music.clip = clip;
        music.loop = isLoop;
        music.Play();
    }

    public int play_sound(string url, bool isLoop = false)
    {
        int soundID = now_soundid;
        AudioClip clip = Resources.Load<AudioClip>(url);
        sounds[now_soundid].clip = clip;
        sounds[now_soundid].loop = isLoop;
        sounds[now_soundid].Play();
        now_soundid++;
        now_soundid = now_soundid > MAX_SOUNDS ? 0 : now_soundid;
        return soundID;
    }

    public void stop_sound(int soundID)
    {
        if (soundID < 0 || soundID >= sounds.Length)
        {
            return;
        }
        sounds[soundID].Stop();
        sounds[soundID].clip = null;
    }

    public void stop_music()
    {
        music.Stop();
        music.clip = null;
    }

    public  void stop_all_sounds()
    {
        for (int i = 0; i < sounds.Length; i++)
        {
            sounds[i].Stop();
            sounds[i].clip = null;
        }
    }

    public void set_music_mute(bool mute)
    {
        if (mute == (music_mute == 1))
            return;

        music_mute = mute ? 1 : 0;
        music.mute = mute;
        PlayerPrefs.SetInt("music_mute", music_mute);
    }

    public void set_sound_mute(bool mute)
    {
        if (mute == (sound_mute == 1))
            return;
        sound_mute = mute ? 1 : 0;
        for (int i = 0; i < MAX_SOUNDS; i++)
        {
            sounds[i].mute = mute;
        }
        PlayerPrefs.SetInt("sound_mute", sound_mute);
    }

    public void set_music_volume(float value)
    {
        if (value == music_volume || value < 0.0f || value > 1.0f)
            return;
        music_volume = value;
        music.volume = value;
        PlayerPrefs.SetFloat("music_volume", music_volume);
    }

    public void set_sound_volume(float value)
    {
        if (value == sound_volume || value < 0.0f || value > 1.0f)
            return;
        for (int i = 0; i < MAX_SOUNDS; i++)
        {
            sounds[i].volume = value;
        }
        sound_volume = value;
        PlayerPrefs.SetFloat("sound_volume", sound_volume);
    }

    public bool musicMuted
    {
        get
        {
            return (music_mute == 1);
        }
    }

    public bool soundMuted
    {
        get
        {
            return (sound_mute == 1);
        }
    }

    public float musicVolume
    {
        get
        {
            return music_volume;
        }
    }

    public float soundVolume
    {
        get
        {
            return sound_volume;
        }
    }
}
